package com.hnkywl.sts.action.sys;

import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.service.xsgl.XsdService;
import com.hnkywl.sts.util.Md5;
import com.hnkywl.sts.util.POIUtil;
import jxl.Sheet;
import jxl.Workbook;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.Region;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 说明：
 * 包:        com.hnkywl.sts.action.sys
 * 类描述:    岗位Action类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Namespace("/sys/yg")
public class YgAction extends SimpleActionSupport {
    private YgService ygService;

    private GsjgService gsjgService;

    private XsdService xsdService;
    //查询条件：员工工号
    private String gh;
    //查询条件：员工姓名
    private String xm;
    //查询条件：员工登录名
    private String dlm;
    //查询条件：员工入职起始时间
    private Date rzsjStart;
    //查询条件：员工入职结束时间
    private Date rzsjEnd;
    //查询条件： 财务编号
    private String cwbh;
    //查询条件： 主管机构
    private Long zgjgId;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows = 10;
    //员工实体
    private Yg yg;
    //当前登录人
    private Yg currentYg;
    private Long ygZgGsId;
    //员工id
    private Long ygId;
    private String validateVal;
    //需要远程ajax验证的属性
    private String validateField;
    //员工选择页面的参数 已选择的岗位Id
    private String selectedIds;
    //主管公司ID
    private Long zggsId;
    //老密码
    private String oldPwd;
    //新密码
    private String newPwd;
    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;

    private File upload;

    private List<String> bmList;


    //员工的查询分页显示
    @Action("ygList")
    public void ygList() {
        this.renderJson(this.ygService.findAllYg(zgjgId, xm, gh, dlm, rzsjStart, rzsjEnd, cwbh, page, rows));
    }

    //员工姓名列表显示
    @Action("ygxm_list")
    public void jsmcList() {
        this.renderJson(this.ygService.findYgXm(xm, 0, -1));
    }

    @Action("yg")
    public String yg() {
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    @Action("ygSearchFromId")
    public void ygSearchFromId() {
        this.renderJson(this.ygService.SearchFromId(ygId));
    }

    //员工的查询分页显示
    @Action("ygXmList")
    public void ygXmList() {
        this.renderJson(this.ygService.findYgListByZgjg(zgjgId));
    }

    //员工在进行级联时返回的值
    @Action("ygAllXmList")
    public void ygAllXmList() {
        this.renderJson(this.ygService.findAllYgListByZgjg(zgjgId));
    }

    //员工的查询分页显示
    @Action("ygListByGs")
    public void ygListByGs() {
        this.renderJson(this.ygService.findYgListByZggs(zgjgId));
    }

    @Action("yg_content")
    public String ygContent() {
        currentYg = YgService.getCurrentYg();
        ygZgGsId = YgService.getCurrentYgZgGsId();
        //进入update页面
        if (ygId != null) {
            yg = ygService.findYgDetail(ygId);
        } else { //带入主管机构
            yg = new Yg();
            yg.setPxh(ygService.getNextPxh() + 1);
            if (zgjgId != null) {
                Gsjg gsjg = gsjgService.findGsjgDetail(zgjgId);
                yg.setSsjgList(new ArrayList<Gsjg>());
                yg.getSsjgList().add(gsjg);
                yg.setZgjg(gsjg);
            }
        }
        return SUCCESS;
    }

    //编码范围设置页面
    @Action("ygbmfwsz")
    public String ygbmfwsz() {
        return SUCCESS;
    }

    //保存编码范围
    @Action("yg-savebmfw")
    public void saveBmfw() {
        try {
            ygService.savewzbmfw(bmList, ygId);
            renderText("{\"statusCode\":200}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":300,\"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("yg_view_content")
    public String ygViewContent() {
        //进入update页面
        ygId = YgService.getCurrentYg().getId();
        yg = ygService.findYgDetail(ygId);
        return SUCCESS;
    }

    /**
     * 方法名:     add_yg
     * 功能描述:      添加员工
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     * 创建人:     scl
     * 创建日期:   13-3-11上午11:50
     * 修改人:     scl
     * 修改日期:   13-3-11上午11:50
     */
    @Action("add_yg")
    public void addYg() {
        try {
            this.ygService.saveYg(yg);
            renderText("{\"statusCode\":\"200\", \"message\":\"添加员工成功!\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     update_yg
     * 功能描述:      修改员工
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     * 创建人:     scl
     * 创建日期:   13-3-11上午11:50
     * 修改人:     scl
     * 修改日期:   13-3-11上午11:50
     */
    @Action("update_yg")
    public void updateYg() {
        try {
            this.ygService.updateYg(yg);
            renderText("{\"statusCode\":\"200\",\"message\":\"修改员工成功!\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }

    }

    /**
     * 远程ajax验证
     */
    @Action("ajax_validate")
    public void ajaxValidate() {
        if (!ygService.ajaxValidate(ygId, validateField, validateVal))
            renderJson("{\"valid\":true}");
        else
            renderJson("{\"valid\":false}");
    }


    /**
     * 方法名:     delete_yg
     * 功能描述:      删除数据库员工
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     * 创建人:     scl
     * 创建日期:   13-3-11上午11:50
     * 修改人:     scl
     * 修改日期:   13-3-11上午11:50
     */
    @Action("delete_yg")
    public void deleteYg() {
        try {
            this.ygService.deleteYg(ygId);
            renderJson("{\"statusCode\":\"200\", \"message\":\"删除员工成功!\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     start_yg
     * 功能描述:      启用员工
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     * 创建人:     scl
     * 创建日期:   13-4-16上午11:50
     * 修改人:     scl
     * 修改日期:   13-4-16上午11:50
     */
    @Action("start_yg")
    public void startYg() {
        try {
            this.ygService.startYg(ygId);
            renderJson("{\"statusCode\":\"200\", \"message\":\"启用成功\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }


    /**
     * 方法名:     stop_yg
     * 功能描述:      停用员工
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     * 创建人:     scl
     * 创建日期:   13-3-11上午11:50
     * 修改人:     scl
     * 修改日期:   13-3-11上午11:50
     */
    @Action("stop_yg")
    public void stopYg() {
        try {
            this.ygService.stopYg(ygId);
            renderJson("{\"statusCode\":\"200\", \"message\":\"停用成功\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("tomodifyYgPassword")
    public String tomodifyYgPassword() {
        ygId = YgService.getCurrentYg().getId();
        return SUCCESS;
    }

    @Action("modifyYgPassword")
    public void modifyYgPassword() {
        try {
            Yg g = this.ygService.findYgDetail(ygId);
            if (g.getDlmm().equals(Md5.md5Encrypt16(oldPwd))) {
                g.setDlmm(Md5.md5Encrypt16(newPwd));
                this.ygService.updatePassword(g);
                renderText("修改密码成功");
//                renderText("{\"statusCode\":\"200\", \"message\":\"修改员工成功\"}");
            } else {
                renderText("请填写正确的员工密码");
//                renderText("{\"statusCode\":\"300\", \"message\":\"请填写正确的员工密码\"}");
            }
        } catch (Exception e) {
            e.printStackTrace();
            renderText(e.getMessage());
//            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 密码重置
     */
    @Action("reset_mm")
    public void resetYg() {
        try {
            this.ygService.resetYg(ygId);
            renderText("重置密码成功");
        } catch (Exception e) {
            e.printStackTrace();
            renderText(e.getMessage());

        }
    }
//    @Action("get_nextpxh")
//    public void getNextPxh() {
//        renderText(ygService.getNextPxh().toString());
//    }

    /**
     * 获取员工科目代码
     */
    @Action("findKmdm")
    public void findKmdm() {
        renderText(ygService.findKmdm(zggsId));
    }

    //员工电话号码导出
    @Action("yg_export")
    public String ygExport() {
        SimpleDateFormat dateformat2 = new SimpleDateFormat("yyyy-MM-dd");
        List<Yg> ygList1 = this.ygService.findAllYgforExport(zgjgId, xm, gh, dlm, rzsjStart, rzsjEnd, cwbh, 1, -1);
        //第一步，创建一个webbook，对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("员工信息");
        POIUtil poiUtil = new POIUtil(wb);
        //设置列宽
        sheet.setDefaultRowHeightInPoints(15);
        sheet.setDefaultColumnWidth((short) 4);
        sheet.setColumnWidth((short) 0, (short) (8 * 512));
        sheet.setColumnWidth((short) 1, (short) (8 * 512));
        sheet.setColumnWidth((short) 2, (short) (8 * 512));
        sheet.setColumnWidth((short) 3, (short) (8 * 512));
        //第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow((int) 0);
        row.setHeightInPoints(36);
        //第四步，创建单元格，并设置值表头  设置表头居中
        HSSFCellStyle titleStyle = poiUtil.createTitleHSSFCellStyle(wb);
        HSSFCell titleCell = row.createCell((short) 0);
        titleCell.setCellStyle(titleStyle);
        titleCell.setEncoding(HSSFCell.ENCODING_UTF_16);
        titleCell.setCellValue("员工信息导出汇总表");
        sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 11));
        row = sheet.createRow((int) 1);
        row.setHeightInPoints(21);
        row = sheet.createRow(2);
        row.setHeightInPoints(15);

        //生成第一行数据
        HSSFCell ygxm = row.createCell((short) 0);
        ygxm.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        ygxm.setEncoding(HSSFCell.ENCODING_UTF_16);
        ygxm.setCellValue("员工姓名");
        HSSFCell zggs = row.createCell((short) 1);
        zggs.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        zggs.setEncoding(HSSFCell.ENCODING_UTF_16);
        zggs.setCellValue("主管公司");
        HSSFCell zgjg = row.createCell((short) 2);
        zgjg.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        zgjg.setEncoding(HSSFCell.ENCODING_UTF_16);
        zgjg.setCellValue("主管机构");
        HSSFCell dhhm = row.createCell((short) 3);
        dhhm.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        dhhm.setEncoding(HSSFCell.ENCODING_UTF_16);
        dhhm.setCellValue("电话号码");

        //表体部分
        for (int i = 3; i < ygList1.size() + 3; i++) {
            Yg k = ygList1.get(i - 3);
            row = sheet.createRow(i);
            row.setHeightInPoints(15);
            ygxm = row.createCell((short) 0);
            ygxm.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            ygxm.setEncoding(HSSFCell.ENCODING_UTF_16);
            ygxm.setCellValue(k.getXm());
            zggs = row.createCell((short) 1);
            zggs.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            zggs.setEncoding(HSSFCell.ENCODING_UTF_16);
            zggs.setCellValue(k.getZggs() == null ? "" : k.getZggs().getJc());
            zgjg = row.createCell((short) 2);
            zgjg.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            zgjg.setEncoding(HSSFCell.ENCODING_UTF_16);
            zgjg.setCellValue(k.getZgjg() == null ? "" : k.getZgjg().getJc());
            dhhm = row.createCell((short) 3);
            dhhm.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            dhhm.setEncoding(HSSFCell.ENCODING_UTF_16);
            dhhm.setCellValue(k.getDhhm() == null ? "" : k.getDhhm());
        }
        try {
            response.setContentType("application/x-download");
            String filedisplay = new String(("员工信息汇总表.xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            wb.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败" + e.getMessage());
        }
        return SUCCESS;
    }

    //员工导入跳转
    @Action("yg_upload")
    public String ygUpload() {
        return SUCCESS;
    }

    //员工电话号码导入
    @Action("yg_import")
    public void ygImport() {
        try {
            SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
            Workbook book = Workbook.getWorkbook(upload);
            //  获得第一个工作表对象
            Sheet sheet = book.getSheet(0);
            //获取总行数
            int rows = sheet.getRows();
            List<Yg> ygList = new ArrayList<Yg>();
            for (int i = 3; i < rows; i++) {
                Yg yg = new Yg();
                yg = ygService.findYgByXm(sheet.getCell(0, i).getContents(), sheet.getCell(1, i).getContents(), sheet.getCell(2, i).getContents());

                if (sheet.getCell(3, i) != null) {
                    yg.setDhhm(sheet.getCell(3, i).getContents());
                } else {
                    yg.setDhhm(null);
                }

                ygList.add(yg);
            }
            this.ygService.saveYgList(ygList);
            renderText("{\"statusCode\":\"200\", \"message\":\"保存成功！\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }
    //================setter getter=========================

    public Yg getYg() {
        return yg;
    }

    public void setYg(Yg yg) {
        this.yg = yg;
    }

    public Yg getCurrentYg() {
        return currentYg;
    }

    public void setCurrentYg(Yg currentYg) {
        this.currentYg = currentYg;
    }

    public Long getYgZgGsId() {
        return ygZgGsId;
    }

    public void setYgZgGsId(Long ygZgGsId) {
        this.ygZgGsId = ygZgGsId;
    }
    //=================setter============================

    public void setZggsId(Long zggsId) {
        this.zggsId = zggsId;
    }

    public void setGh(String gh) {
        this.gh = gh;
    }

    public void setXm(String xm) {
        this.xm = xm;
    }

    public void setDlm(String dlm) {
        this.dlm = dlm;
    }

    public void setRzsjStart(Date rzsjStart) {
        this.rzsjStart = rzsjStart;
    }

    public void setRzsjEnd(Date rzsjEnd) {
        this.rzsjEnd = rzsjEnd;
    }

    public void setCwbh(String cwbh) {
        this.cwbh = cwbh;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public void setSelectedIds(String selectedIds) {
        this.selectedIds = selectedIds;
    }

    public Long getYgId() {
        return ygId;
    }

    public void setYgId(Long ygId) {
        this.ygId = ygId;
    }

    public Long getZgjgId() {
        return zgjgId;
    }

    public void setZgjgId(Long zgjgId) {
        this.zgjgId = zgjgId;
    }

    public String getValidateVal() {
        return validateVal;
    }

    public void setValidateVal(String validateVal) {
        this.validateVal = validateVal;
    }

    public String getValidateField() {
        return validateField;
    }

    public void setValidateField(String validateField) {
        this.validateField = validateField;
    }

    public String getOldPwd() {
        return oldPwd;
    }

    public void setOldPwd(String oldPwd) {
        this.oldPwd = oldPwd;
    }

    public String getNewPwd() {
        return newPwd;
    }

    public void setNewPwd(String newPwd) {
        this.newPwd = newPwd;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public List<String> getBmList() {
        return bmList;
    }

    public void setBmList(List<String> bmList) {
        this.bmList = bmList;
    }

    @Autowired
    public void setYgService(YgService ygService) {
        this.ygService = ygService;
    }

    @Autowired
    public void setGsjgService(GsjgService gsjgService) {
        this.gsjgService = gsjgService;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setXsdService(XsdService xsdService) {
        this.xsdService = xsdService;
    }
}
